﻿using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Cailiao
{
    /// <summary>
    /// Home.xaml 的交互逻辑
    /// </summary>
    public partial class Home : Page
    {
        ViewModel.MaterialViewModel vm;
        Model.DBContext dBContext = new Model.DBContext();
        public Home()
        {
            InitializeComponent();
            vm =this.DataContext as ViewModel.MaterialViewModel;

            //获取数据
            vm.SumCount = dBContext.DB.Queryable<Model.MaterialBill>().Count();
            vm.NewCount = dBContext.DB.Queryable<Model.MaterialBill>().Where(m => m.State=="待处理").Count();
            vm.CompleteCount = dBContext.DB.Queryable<Model.MaterialBill>().Where(m => m.State=="已通过").Count();

            //集合数据
            //List<int> listInt=db.Queryable<Student>().Select(it => it.Id).ToList();
            vm.NewList = new List<int>();
            vm.HandleList = new List<int>();
            vm.XiaolvList = new List<int>();


            //近7天新增申请
            for (int i = 0; i <= 7; i++)
            {
                vm.NewList.Add(dBContext.DB.Queryable<Model.MaterialBill>().Where(m=>m.CreateTime > DateTime.Now.Date.AddDays(i-7)  &&  m.CreateTime <DateTime.Now.Date.AddDays((i-7)+1)).Count());
            }

            //近7天通过申请
            for (int i = 0; i <= 7; i++)
            {
                vm.HandleList.Add(dBContext.DB.Queryable<Model.MaterialBill>().Where(m=>m.State=="已通过").Where(m => m.CreateTime > DateTime.Now.Date.AddDays(i - 7) && m.CreateTime < DateTime.Now.Date.AddDays((i - 7) + 1)).Count());
            }

            //处理材料排行
            //var ItemList = dBContext.DB.Queryable<Model.MaterialBill>().Select(m=> new {m.Id,m.ItemName,m.ItemCount }).GroupBy(m => new  { m.Id,m.ItemName,m.ItemCount}).ToList().OrderByDescending(m=>m.ItemCount).Take(7);
            //var itemNameList =new  List<string>();
            //foreach (var item in ItemList)
            //{
            //    vm.XiaolvList.Add((int)item.ItemCount);
            //    itemNameList.Add(item.ItemName);
            //}

            

            for (int i = 0; i <= 7; i++)
            {
                vm.WeekLabels.Add(DateTime.Now.Date.AddDays(i - 7).Date.ToString("MM-dd"));
            }

            cc_Zhucai.Series.Add(new ColumnSeries
            {
                Values = new ChartValues<int>(vm.NewList), 
                DataLabels = true,
                LabelPoint = point => point.Y + ""
            }) ; 

            cc_Zhucai.AxisX.Add(new Axis
            {
                Labels = vm.WeekLabels,
              
                Separator = new LiveCharts.Wpf.Separator // force the separator step to 1, so it always display all labels
                {
                    Step = 1,
                    IsEnabled = false //disable it to make it invisible.
                },
                LabelsRotation = 15
            });

            cc_Zhucai.AxisY.Add(new Axis
            {
                LabelFormatter = value => value + "",
                Separator = new LiveCharts.Wpf.Separator()
            });


            cc_Chuli.Series.Add(new ColumnSeries
            {
                Values = new ChartValues<int>(vm.HandleList),
                DataLabels = true,
                LabelPoint = point => point.Y + ""
            });

            cc_Chuli.AxisX.Add(new Axis
            {
                Labels = vm.WeekLabels,

                Separator = new LiveCharts.Wpf.Separator // force the separator step to 1, so it always display all labels
                {
                    Step = 1,
                    IsEnabled = false //disable it to make it invisible.
                },
                LabelsRotation = 15
            });

            cc_Chuli.AxisY.Add(new Axis
            {
                LabelFormatter = value => value + "",
                Separator = new LiveCharts.Wpf.Separator()
            });



            //cc_tang.Series.Add(new ColumnSeries
            //{
            //    Values = new ChartValues<int>(vm.HandleList),
            //    DataLabels = true,
            //    LabelPoint = point => point.Y + ""
            //});

            //cc_tang.AxisX.Add(new Axis
            //{
            //    Labels = itemNameList,

            //    Separator = new LiveCharts.Wpf.Separator // force the separator step to 1, so it always display all labels
            //    {
            //        Step = 1,
            //        IsEnabled = false //disable it to make it invisible.
            //    },
            //    LabelsRotation = 15
            //});
            //cc_tang.AxisY.Add(new Axis
            //{
            //    LabelFormatter = value => value + "",
            //    Separator = new LiveCharts.Wpf.Separator()
            //});


        }
    }
}
